package ejemplosgui.matisse;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

enum TipoOperacion {

    SUMA,
    RESTA,
    MULTIPLICACION,
    DIVISION,
    NINGUNA,
    INICIALIZACION;
}

public class Calculadora extends javax.swing.JFrame implements ActionListener {

    private String teclado[] = {"1", "2", "3", "+",
        "4", "5", "6", "-",
        "7", "8", "9", "*",
        "0", "=", ".", "/"
    };
    private TipoOperacion codigoDeOperacion = TipoOperacion.INICIALIZACION;
    private float dato = 0;

    public Calculadora() {
        initComponents();
        for (int i = 0; i < teclado.length; i++) {
            JButton boton = new JButton(teclado[i]);
            boton.setActionCommand(teclado[i]);
            jPanel1.add(boton);
            boton.addActionListener(this);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        displayTextField = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 51));
        jLabel1.setText("Calculadora");

        displayTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        displayTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        displayTextField.setText("0");

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setMinimumSize(new java.awt.Dimension(200, 200));
        jPanel1.setLayout(new java.awt.GridLayout(4, 4, 4, 4));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(136, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(124, 124, 124))
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(83, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(displayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(108, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(displayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Calculadora().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField displayTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        
        String actionCommand = e.getActionCommand();
        if (actionCommand.equals("+")) {
            codigoDeOperacion = TipoOperacion.SUMA;
            dato = Float.parseFloat(displayTextField.getText());
            displayTextField.setText("");
        } else if (actionCommand.equals("-")) {
            codigoDeOperacion = TipoOperacion.RESTA;
            dato = Float.parseFloat(displayTextField.getText());
            displayTextField.setText("");
        } else if (actionCommand.equals("*")) {
            codigoDeOperacion = TipoOperacion.MULTIPLICACION;
            dato = Float.parseFloat(displayTextField.getText());
            displayTextField.setText("");
        } else if (actionCommand.equals("/")) {
            codigoDeOperacion = TipoOperacion.DIVISION;
            dato = Float.parseFloat(displayTextField.getText());
            displayTextField.setText("");
        } else if (actionCommand.equals("=")) {
            float dato2 = Float.parseFloat(displayTextField.getText());
            switch (codigoDeOperacion) {
                case SUMA : {
                    displayTextField.setText("" + (dato + dato2));
                    break;
                }
                case RESTA: {
                    displayTextField.setText("" + (dato - dato2));
                    break;
                }
                case MULTIPLICACION: {
                    displayTextField.setText("" + (dato * dato2));
                    break;
                }
                case DIVISION: {
                    displayTextField.setText("" + (dato / dato2));
                    break;
                }
            }
            codigoDeOperacion = TipoOperacion.NINGUNA;

        } else {
            if (codigoDeOperacion == TipoOperacion.INICIALIZACION) {
                displayTextField.setText(actionCommand);
                codigoDeOperacion = TipoOperacion.NINGUNA;
            } else {
                displayTextField.setText(displayTextField.getText() + actionCommand);
            }
        }
    }

}
